package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.core.dao.read.member.MemberCopartnerCountReadDao;
import com.party.core.dao.write.member.MemberCopartnerCountWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.member.CopartnerAnalyzeQuery;
import com.party.core.model.member.MemberCopartnerCount;
import com.party.core.model.member.MemberCopartnerCountWithMember;
import com.party.core.service.member.IMemberCopartnerCountService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 合伙人数据统计总计接口实现
 * Created by wei.li.
 *
 * @date 2018/1/16 0016
 * @time 10:51
 */
@Service
public class MemberCopartnerCountService implements IMemberCopartnerCountService {

    @Autowired
    private MemberCopartnerCountWriteDao memberCopartnerCountWriteDao;

    @Autowired
    private MemberCopartnerCountReadDao memberCopartnerCountReadDao;

    /**
     * 新增
     *
     * @param memberCopartnerCount 统计数据
     * @return 编号
     */
    @Override
    public String insert(MemberCopartnerCount memberCopartnerCount) {
        BaseModel.preInsert(memberCopartnerCount);
        boolean result = memberCopartnerCountWriteDao.insert(memberCopartnerCount);
        if (result) {
            return memberCopartnerCount.getId();
        }
        return null;
    }

    /**
     * 更新
     *
     * @param memberCopartnerCount 统计数据
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MemberCopartnerCount memberCopartnerCount) {
        return memberCopartnerCountWriteDao.update(memberCopartnerCount);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberCopartnerCountWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberCopartnerCountWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberCopartnerCountWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberCopartnerCountWriteDao.batchDelete(ids);
    }

    /**
     * 获取合伙人统计
     *
     * @param id 主键
     * @return 统计详情
     */
    @Override
    public MemberCopartnerCount get(String id) {
        return memberCopartnerCountReadDao.get(id);
    }

    /**
     * 获取或者设置
     *
     * @param memberId 会员编号
     * @return 合伙人统计总计
     */
    @Override
    public MemberCopartnerCount getSet(String memberId) {
        MemberCopartnerCount memberCopartnerCount = this.findByMemberId(memberId);
        if (null != memberCopartnerCount) {
            return memberCopartnerCount;
        } else {
            memberCopartnerCount = new MemberCopartnerCount(0, 0, 0, 0d, 0d, 0d, 0d, 0d);
            memberCopartnerCount.setMemberId(memberId);
            this.insert(memberCopartnerCount);
        }
        return memberCopartnerCount;
    }

    /**
     * 根据
     *
     * @param memberId 会员编号
     * @return
     */
    @Override
    public MemberCopartnerCount findByMemberId(String memberId) {
        MemberCopartnerCount memberCopartnerCount = memberCopartnerCountReadDao.findByMemberId(memberId);
//        if (null == memberCopartnerCount) {
//            memberCopartnerCount = new MemberCopartnerCount(0, 0, 0, 0d, 0d, 0d);
//        }
        return memberCopartnerCount;
    }

    /**
     * 分页查询
     *
     * @param memberCopartnerCount 合伙人统计总计
     * @param page                 分页信息
     * @return 统计列表
     */
    @Override
    public List<MemberCopartnerCount> listPage(MemberCopartnerCount memberCopartnerCount, Page page) {
        return memberCopartnerCountReadDao.listPage(memberCopartnerCount, page);
    }

    /**
     * 查询所有
     *
     * @param memberCopartnerCount 合伙人统计总计
     * @return 统计列表
     */
    @Override
    public List<MemberCopartnerCount> list(MemberCopartnerCount memberCopartnerCount) {
        return memberCopartnerCountReadDao.listPage(memberCopartnerCount, null);
    }

    @Override
    public List<MemberCopartnerCount> batchList(@NotNull Set<String> ids, MemberCopartnerCount memberCopartnerCount, Page page) {
        return null;
    }

    /**
     * 根据条件查询
     *
     * @param copartnerAnalyzeQuery 查询参数
     * @param page                  分页参数
     * @return 查询结果
     */
    @Override
    public List<MemberCopartnerCountWithMember> queryList(CopartnerAnalyzeQuery copartnerAnalyzeQuery, Page page) {
        return memberCopartnerCountReadDao.queryList(copartnerAnalyzeQuery, page);
    }

    /**
     * 统计所有
     *
     * @return 统计数据
     */
    @Override
    public MemberCopartnerCount sum(MemberCopartnerCount count) {
        return memberCopartnerCountReadDao.sum(count);
    }

    @Override
    public void updateBrokerage(String memberId, Double brokerage, Boolean isContain, boolean isBegin, double withdrawalTotal) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getTotalBrokerage()) {
            double add = BigDecimalUtils.add(memberCopartnerCount.getTotalBrokerage(), brokerage);
            double round = BigDecimalUtils.round(add, 2);
            memberCopartnerCount.setTotalBrokerage(round);
        } else {
            memberCopartnerCount.setTotalBrokerage(0d);
        }

        if (!isContain) {
            if (null != memberCopartnerCount.getMyBrokerage()) {
                double add = BigDecimalUtils.add(memberCopartnerCount.getMyBrokerage(), brokerage);
                double round = BigDecimalUtils.round(add, 2);
                memberCopartnerCount.setMyBrokerage(round);
            } else {
                memberCopartnerCount.setMyBrokerage(0d);
            }
        }

//        if (isBegin) {
//            // 可结算
//            double balance = BigDecimalUtils.sub(memberCopartnerCount.getTotalBrokerage(), memberCopartnerCount.getNotBalance());
//            // 可提现
//            double canWithdrawal = BigDecimalUtils.sub(balance, withdrawalTotal);
//            memberCopartnerCount.setCanWithdrawal(BigDecimalUtils.round(canWithdrawal, 2));
//
//
//        } else {
//            // 未结算
//            if (null != memberCopartnerCount.getNotBalance()) {
//                double add = BigDecimalUtils.add(memberCopartnerCount.getNotBalance(), brokerage);
//                memberCopartnerCount.setNotBalance(BigDecimalUtils.round(add, 2));
//            } else {
//                memberCopartnerCount.setNotBalance(0d);
//            }
//        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateSuccess(String projectId, String memberId) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getSuccessNum()) {
            memberCopartnerCount.setSuccessNum(memberCopartnerCount.getSuccessNum() + 1);
        } else {
            memberCopartnerCount.setSuccessNum(1);
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateApplyNum(String projectId, String memberId) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getApplyNum()) {
            memberCopartnerCount.setApplyNum(memberCopartnerCount.getApplyNum() + 1);
        } else {
            memberCopartnerCount.setApplyNum(1);
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateApplyRefund(String memberId, double payment) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        memberCopartnerCount.setApplyNum(memberCopartnerCount.getApplyNum() - 1);
        if (payment > 0) {
            memberCopartnerCount.setOrderNum(memberCopartnerCount.getOrderNum() - 1);
            double subTotalMoney = BigDecimalUtils.sub(memberCopartnerCount.getTotalMoney(), payment);
            memberCopartnerCount.setTotalMoney(BigDecimalUtils.round(subTotalMoney, 2));
        }

        if (memberCopartnerCount.getApplyNum() < 0) {
            memberCopartnerCount.setApplyNum(0);
        }
        if (memberCopartnerCount.getOrderNum() < 0) {
            memberCopartnerCount.setOrderNum(0);
        }
        if (memberCopartnerCount.getTotalMoney() < 0) {
            memberCopartnerCount.setTotalMoney(0d);
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateApplyRefundBrokerage(String memberId, float brokerage, boolean isMyBrokerage, boolean isBegin,
                                           double withdrawalTotal) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        double subTotalBrokerage = BigDecimalUtils.sub(memberCopartnerCount.getTotalBrokerage(), brokerage);
        memberCopartnerCount.setTotalBrokerage(subTotalBrokerage);
        if (isMyBrokerage) {
            double subMyBrokerage = BigDecimalUtils.sub(memberCopartnerCount.getMyBrokerage(), brokerage);
            memberCopartnerCount.setMyBrokerage(subMyBrokerage);
        }

        if (isBegin) {
            // 可结算
            double balance = BigDecimalUtils.sub(memberCopartnerCount.getTotalBrokerage(), memberCopartnerCount.getNotBalance());
            // 可提现
            double canWithdrawal = BigDecimalUtils.sub(balance, withdrawalTotal);
            memberCopartnerCount.setCanWithdrawal(canWithdrawal);
        } else {
            // 不可提现
            double subNotBalance = BigDecimalUtils.sub(memberCopartnerCount.getNotBalance(), brokerage);
            memberCopartnerCount.setNotBalance(subNotBalance);
        }
        if (memberCopartnerCount.getTotalBrokerage() < 0) {
            memberCopartnerCount.setTotalBrokerage(0d);
        }
        if (memberCopartnerCount.getMyBrokerage() < 0) {
            memberCopartnerCount.setMyBrokerage(0d);
        }
        if (memberCopartnerCount.getNotBalance() < 0) {
            memberCopartnerCount.setNotBalance(0d);
        }
        if (memberCopartnerCount.getCanWithdrawal() < 0) {
            memberCopartnerCount.setCanWithdrawal(0d);
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateOrderAnalyze(Float payment, String memberId) {
        //统计总计
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getOrderNum()) {
            memberCopartnerCount.setOrderNum(memberCopartnerCount.getOrderNum() + 1);
        } else {
            memberCopartnerCount.setOrderNum(1);
        }
        if (null != memberCopartnerCount.getTotalMoney()) {
            double add = BigDecimalUtils.add(memberCopartnerCount.getTotalMoney(), payment);
            double round = BigDecimalUtils.round(add, 2);
            memberCopartnerCount.setTotalMoney(round);
        } else {
            memberCopartnerCount.setTotalMoney(Double.valueOf(payment));
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateOrderAnalyzeSub(Float payment, String memberId) {
        //统计总计
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getOrderNum()) {
            memberCopartnerCount.setOrderNum(memberCopartnerCount.getOrderNum() - 1);
            if (memberCopartnerCount.getOrderNum() < 0) {
                memberCopartnerCount.setOrderNum(0);
            }
        } else {
            memberCopartnerCount.setOrderNum(0);
        }
        if (null != memberCopartnerCount.getTotalMoney()) {
            double add = BigDecimalUtils.sub(memberCopartnerCount.getTotalMoney(), payment);
            double round = BigDecimalUtils.round(add, 2);
            memberCopartnerCount.setTotalMoney(round);
            if (memberCopartnerCount.getTotalMoney() < 0d) {
                memberCopartnerCount.setTotalMoney(0d);
            }
        } else {
            memberCopartnerCount.setTotalMoney(Double.valueOf(payment));
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateSuccessSub(String projectId, String memberId) {
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        if (null != memberCopartnerCount.getSuccessNum()) {
            memberCopartnerCount.setSuccessNum(memberCopartnerCount.getSuccessNum() - 1);
        } else {
            memberCopartnerCount.setSuccessNum(0);
        }
        this.update(memberCopartnerCount);
    }

    @Override
    public void updateCrowdfundSuccess(Float payment, String memberId) {
        //统计总计
        MemberCopartnerCount memberCopartnerCount = this.getSet(memberId);
        memberCopartnerCount.setOrderNum(memberCopartnerCount.getOrderNum() + 1);

        double add = BigDecimalUtils.add(memberCopartnerCount.getTotalMoney(), payment);
        double round = BigDecimalUtils.round(add, 2);
        memberCopartnerCount.setTotalMoney(round);

        memberCopartnerCount.setSuccessNum(memberCopartnerCount.getSuccessNum() + 1);

        this.update(memberCopartnerCount);
    }
}
